package com.item.util;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.*;


public class XmlParser {
	private static File xmlFile = null;
	private static DocumentBuilder builder = null;
	private static DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	private static Document doc = null;
	private final static String path ="/xmls/";
	private final static String schoolXmlPath = path  + "SchoolBasicInfo.xml";
	private final static String projectXmlPath = path + "projectInfo.xml";
	public XmlParser() {
	}
	static {
		try {
			//����xmlFile��λ�õ�webapps��classes�ļ�����
			builder = factory.newDocumentBuilder();
						
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String getFirstTermBegin(String id) {
		String date = "09-02";
		System.out.println(schoolXmlPath);
		xmlFile = new File(schoolXmlPath);
	
		try {
			doc = builder.parse(xmlFile);
			Element school = doc.getElementById(id);
			NodeList childNodes = school.getChildNodes();
			for(int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i);
				if("first-term-begin".equalsIgnoreCase(node.getNodeName())) {
					if(node.getNodeName()!=null)
						date = node.toString();
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static String getSecondTermBegin(String id) {
		String date = "02-17";
		xmlFile = new File(schoolXmlPath);
		try {
			doc = builder.parse(xmlFile);
			Element school = doc.getElementById(id);
			NodeList childNodes = school.getChildNodes();
			for(int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i);
				if("second-term-begin".equalsIgnoreCase(node.getNodeName())) 
					if(node.getNodeValue() != null)
						date = node.getNodeValue();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * ��ȡ���������ϵͳ��¼�˺�
	 * @param String id
	 * @return String
	 */
	public static String getUndergraduateUsername(String id) {
		String username = "101070056";
		xmlFile = new File(schoolXmlPath);
		try {
			doc = builder.parse(xmlFile);
			Element school = doc.getElementById(id);
			NodeList childNodes = school.getChildNodes();
			for(int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i);
				if("undergraduate".equalsIgnoreCase(node.getNodeName())) {
					NamedNodeMap attrs = node.getAttributes();
					username = attrs.getNamedItem("username").getNodeValue();
				}				
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return username;
	}
	
	/**
	 * ��ȡ���������ϵͳ��¼����
	 * @param String id
	 * @return String
	 */
	public static String getUndergraduatePassword(String id) {
		String password = "236676";
		xmlFile = new File(schoolXmlPath);
		try {
			doc = builder.parse(xmlFile);
			Element school = doc.getElementById(id);
			NodeList childNodes = school.getChildNodes();
			for(int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i);
				if("undergraduate".equalsIgnoreCase(node.getNodeName())) {
					NamedNodeMap attrs = node.getAttributes();
					password = attrs.getNamedItem("password").getNodeValue();
				}				
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return password;
	}
	
	/**
	 * ��ȡ�о������ϵͳ��¼�˺�
	 * @param String id
	 * @return String
	 */
	public static String getGraduateUsername(String id) {
		String username = "Mg1314027";
		xmlFile = new File(schoolXmlPath);
		try {
			doc = builder.parse(xmlFile);
			Element school = doc.getElementById(id);
			NodeList childNodes = school.getChildNodes();
			for(int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i);
				if("graduate".equalsIgnoreCase(node.getNodeName())) {
					NamedNodeMap attrs = node.getAttributes();
					username = attrs.getNamedItem("username").getNodeValue();
				}				
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return username;
	}
	
	/**
	 * ��ȡ�о������ϵͳ��¼����
	 * @param String id
	 * @return String
	 */
	public static String getGraduatePassword(String id) {
		String password = "194412";
		xmlFile = new File(schoolXmlPath);
		try {
			doc = builder.parse(xmlFile);
			Element school = doc.getElementById(id);
			NodeList childNodes = school.getChildNodes();
			for(int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i);
				if("graduate".equalsIgnoreCase(node.getNodeName())) {
					NamedNodeMap attrs = node.getAttributes();
					password = attrs.getNamedItem("password").getNodeValue();
				}				
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return password;
	}
	
	/**
	 * ��ȡ�û�ͷ������Ŀ¼
	 * @param
	 * @return String
	 */
	public static String getUserAvatarDir() {
		String directory = "avatar";
		xmlFile = new File(projectXmlPath);
		try {
			doc = builder.parse(xmlFile);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes();
			for(int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i);
				if("userInfo".equalsIgnoreCase(node.getNodeName())) {
					NamedNodeMap attrs = node.getAttributes();
					directory = attrs.getNamedItem("avatarPath").getNodeValue();
				}				
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return directory;
	}
	
	/**
	 * ��ȡ��Ŀ���
	 * @param
	 * @return String
	 */
	public static String getProjectName() {
		String directory = "";
		xmlFile = new File(projectXmlPath);
		try {
			doc = builder.parse(xmlFile);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes();
			for(int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i);
				if("project-name".equalsIgnoreCase(node.getNodeName())) {
					NamedNodeMap attrs = node.getAttributes();
					directory = attrs.getNamedItem("name").getNodeValue();
				}				
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return directory;
	}
	/**
	 * ��ȡIP��ַ
	 * @param
	 * @return String
	 */
	public static String getProjectURL() {
		String directory = "";
		xmlFile = new File(projectXmlPath);
		try {
			doc = builder.parse(xmlFile);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes();
			for(int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i);
				if("project-url".equalsIgnoreCase(node.getNodeName())) {
					NamedNodeMap attrs = node.getAttributes();
					directory = attrs.getNamedItem("name").getNodeValue();
				}				
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return directory;
	}
}
